# -*- coding: utf-8 -*-
# 导如第三方库
import tool

import pandas as pd

from pyecharts.charts import Bar, Map,Timeline

from pyecharts import options as opts

################
# 出品年份分析 #
################
# 使用读取csv表格数据
data = pd.read_csv('data.csv', encoding='gbk', usecols=["name", "year", "score", "country"])

# 按年份分组统计当年电影数量 并转化为字典
count = data['year'].value_counts().to_dict()

# 引入pyecharts柱状图
bar = Bar()

# 添加图表x轴数据
bar.add_xaxis(list(count.keys()))

# 添加图表y轴数据
bar.add_yaxis("优秀电影数量", list(count.values()))

# 设置图表标题信息
bar.set_global_opts(title_opts=opts.TitleOpts(title="Top250优秀电影数量出品年份统计", subtitle="年份/数量"))

# 绘制生成图表
bar.render('year.html')

#############
#  时空分析 #
#############
year = data['year'].value_counts().to_dict().keys()

year = list(year)

year.sort()

tl = Timeline()

for i in year:
    row = data.loc[data["year"] == i]
    map = tool.count_country(row["country"].to_list())

    # 引入pyecharts柱状图
    bar = Bar()

    # 添加图表x轴数据
    bar.add_xaxis(list(map.keys()))

    # 添加图表y轴数据
    bar.add_yaxis("高分电影数量", list(map.values()))

    # 设置图表标题信息
    bar.set_global_opts(title_opts=opts.TitleOpts(title="{}年国家出品高分电影数量".format(i), subtitle="年份/数量"))

    tl.add(bar, "{}年".format(i))

tl.render('time.html')

#############
#  空间分析 #
#############
year = data['year'].value_counts().to_dict().keys()
year = list(year)
year.sort()
map = tool.count_country(data["country"].to_list())
map0 = (
    Map()
        .add("", [list(z) for z in zip(map.keys(), map.values())], "world", is_map_symbol_show=False)  # 以列表形式存放数据
        .set_global_opts(
        title_opts=opts.TitleOpts(title="高分电影出品国家分布分析"),
        visualmap_opts=opts.VisualMapOpts(max_=200),
    ).render('map.html')
)

#############
#  时空地图 #
#############
year = data['year'].value_counts().to_dict().keys()

year = list(year)

year.sort()

tl = Timeline()

for i in year:
    row = data.loc[data["year"] == i]
    map = tool.count_country(row["country"].to_list())
    map0 = (
        Map()
            .add("", [list(z) for z in zip(map.keys(), map.values())], "world", is_map_symbol_show=False)  # 以列表形式存放数据
            .set_global_opts(
            title_opts=opts.TitleOpts(title="Map{}年高分电影出品国家".format(i)),
            visualmap_opts=opts.VisualMapOpts(max_=200),
        )
    )

    tl.add(map0, "{}年".format(i))

tl.render('timemap.html')
